import Vue from 'vue';
import Vuex from 'vuex';
import { SOME_MUTATION  } from './type';
// Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    nettrafficTabData: [
      {
        id: 1,
        dateTime: '2019-12-02',
        platfromName: '今日阅读',
        pv: 330,
        uv: 220,
        ip: 340,
        pv_uv: 1.5,
        pv_ip: 2.4,
        uvRate: "5%",
        adClickRate: "4%",
        secondShareCount: 300,
        fromFriendRate: "6%",
        fromGroupRate: "4%",
        fromTimeLineRate: "3%",
        expandAllArt: 300,
        readArtCount: 400,
        addScoreAction: 200,
        valuedScore: 120,
        singleIpGlod: 1000,
        nineRate: "20%",
        singleRoll: '100ms',
        waitPageTime: '200ms',
        falseAdTime: '120ms',
        trueAdTime: '20ms',
        backRate: '50ms',
        scanQrcodeRate: '60%',
        children: [
          {
            id: 2,
            dateTime: '2019-12-02',
            platfromName: '今日阅读',
            pv: 330,
            uv: 220,
            ip: 340,
            pv_uv: 1.5,
            pv_ip: 2.4,
            uvRate: "5%",
            adClickRate: "4%",
            secondShareCount: 300,
            fromFriendRate: "6%",
            fromGroupRate: "4%",
            fromTimeLineRate: "3%",
            expandAllArt: 300,
            readArtCount: 400,
            addScoreAction: 200,
            valuedScore: 120,
            singleIpGlod: 1000,
            nineRate: "20%",
            singleRoll: '100ms',
            waitPageTime: '200ms',
            falseAdTime: '120ms',
            trueAdTime: '20ms',
            backRate: '50ms',
            scanQrcodeRate: '60%',
          }]
      }
    ],
    nettrafficDiaTab1:[

    ],
    nettrafficDiaTab2:[

    ],
    saleTableData:[],

    todos:[
      {
        id:1,
        title:'test',
        content:'this is a test state',
        done:true
      },
      {
        id:2,
        title:'test',
        content:'this is a test state2',
        done:false
      }
    ],
    count: 1,
    obj:{
      name:'luban',
      age:26,
      address:'shanghai city'
    }
  },
  getters: {
    //调用方法:store.getters.doneTodos // -> [{ id: 1, text: '...', done: true }]
    doneTodos: state => {
      return state.todos.filter(todo => todo.done)
    },
    //调用方法:store.getters.doneTodosCount // -> 1
    doneTodosCount: (state, getters) => {
      return getters.doneTodos.length
    },
    //调用方法:store.getters.getTodoById(2) // -> { id: 2, text: '...', done: false }
    getTodoById: (state) => (id) => {
      return state.todos.find(todo => todo.id === id)
    },
  },
  //mutations必须为同步函数
  mutations: {
    //调用方法:store.commit('countAdd')
    countAdd (state) {
      // 变更状态
      state.count++
    },
    //调用方法:store.commit('increment', 10)
    increment (state, n) {
      state.count += n
    },
    //调用方法:store.commit('getParamsCount', {
    //   amount: 10
    //可以传入多个参数的一个对象
    // })
    getParamsCount(state, payload){
      state.count += payload.amount
    },
    //对象风格的调用:store.commit({
    //   type: 'objStyleCommit',
    //   amount: 10
    // })
    objStyleCommit(state,payload){
      state.count += payload.amount
    },
    // 我们可以使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名
    [SOME_MUTATION] (state) {
      // mutate state
      state.obj = { ...state.obj, newProp: 123 };
    }
  },
  //1.Action 提交的是 mutation，而不是直接变更状态。 2.Action 可以包含任意异步操作。
  actions: {
    [SOME_MUTATION] (context) {
      context.commit('increment')
    }
  },
  modules: {}
})
